package com.enjoyor.soa.traffic.core.efoms.service.impl;

import com.enjoyor.soa.traffic.core.efoms.domain.DeviceStatusInfo;
import com.enjoyor.soa.traffic.core.efoms.enums.EnumDicPublic;
import com.enjoyor.soa.traffic.core.efoms.mapper.IDeviceStatusInfoMapper;
import com.enjoyor.soa.traffic.core.efoms.service.IDevStatusInfoService;
import com.enjoyor.soa.traffic.frame.logger.IPubLogMapper;
import com.enjoyor.soa.traffic.util.enums.log.LogInfoDic;
import com.enjoyor.soa.traffic.util.helper.LoggerHelper;
import com.enjoyor.soa.traffic.util.helper.TimeHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;


@Service
@Transactional
public class DevStatusInfoService implements IDevStatusInfoService {

    @Autowired
    private IDeviceStatusInfoMapper deviceStatusInfoMapper;

    @Autowired
    private IPubLogMapper pubLogMapper;

    @Value("${log.system.name}")
    private String systemName;
    
    /**
     * 新增设备状态信息表（关键字段：设备编号、设备名称、设备类别、设备类别名称、设备状态编号、设备装态）
     *
     * @param deviceStatusInfo
     * @param username
     * @return
     */
    @Override
    public String insertDevStatusInfoOnly(DeviceStatusInfo deviceStatusInfo, String username) {
        try{
        	String devStatusId = "S" + TimeHelper.getUniqueString();
    		deviceStatusInfo.setDevStatusId(devStatusId);
        	if(null == deviceStatusInfo.getDevStatusCode() || "".equals(deviceStatusInfo.getDevStatusCode())) {
        		deviceStatusInfo.setDevStatusCode(EnumDicPublic.DEVICE_STATUS_NAOMAL.getKey());
        		deviceStatusInfo.setDevStatusName(EnumDicPublic.DEVICE_STATUS_NAOMAL.getValue());
        	}
        	if(null == deviceStatusInfo.getRepStatusCode() || "".equals(deviceStatusInfo.getRepStatusCode())) {
        		deviceStatusInfo.setRepStatusCode(EnumDicPublic.DEV_REP_STATUS_USEING.getKey());
        		deviceStatusInfo.setRepStatusName(EnumDicPublic.DEV_REP_STATUS_USEING.getValue());
        	}
        	deviceStatusInfo.setUpdateTime(TimeHelper.dateToLongString());
            deviceStatusInfoMapper.insert(deviceStatusInfo);
            pubLogMapper.saveNoramlLogInfo(username, systemName,LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_INSERT.getCode(),
                    String.format("添加设备状态编号:{%s} 操作成功!", new Object[] { devStatusId }),
                    LogInfoDic.LOG_MODULAR_ENUM.TEMPLATE_TEST_MANAGEMENT.getMessage());
        	return devStatusId;
        }catch (Exception e){
            System.out.println("deviceStatusInfo = [" + deviceStatusInfo + "], username = [" + username + "]");
            throw e;
        }

    }
    
    /**
     * 判断是否存在，新增/更新设备状态信息表（关键字段：设备编号、设备名称、设备类别、设备类别名称、设备状态编号、设备装态）
     *
     * @param deviceStatusInfo
     * @param username
     * @return
     */
    @Override
    public String insertDevStatusInfo(DeviceStatusInfo deviceStatusInfo, String username) {
        try{
        	String devStatusId = null;
        	DeviceStatusInfo devStatus = this.getDevStatusInfo(deviceStatusInfo.getDevCategoryCode(),
        			deviceStatusInfo.getDevTypeCode(),deviceStatusInfo.getDevId());
        	if(devStatus != null) {
        		this.updateDevStatusInfo(deviceStatusInfo, username);
        	}else {
        		devStatusId = "S"+TimeHelper.getUniqueId(4);
        		deviceStatusInfo.setDevStatusId(devStatusId);
	        	if(null == deviceStatusInfo.getDevStatusCode() || "".equals(deviceStatusInfo.getDevStatusCode())) {
	        		deviceStatusInfo.setDevStatusCode(EnumDicPublic.DEVICE_STATUS_NAOMAL.getKey());
	        		deviceStatusInfo.setDevStatusName(EnumDicPublic.DEVICE_STATUS_NAOMAL.getValue());
	        	}
	        	if(null == deviceStatusInfo.getRepStatusCode() || "".equals(deviceStatusInfo.getRepStatusCode())) {
	        		deviceStatusInfo.setRepStatusCode(EnumDicPublic.DEV_REP_STATUS_USEING.getKey());
	        		deviceStatusInfo.setRepStatusName(EnumDicPublic.DEV_REP_STATUS_USEING.getValue());
	        	}
	        	deviceStatusInfo.setUpdateTime(TimeHelper.dateToLongString());
	            deviceStatusInfoMapper.insert(deviceStatusInfo);
	            pubLogMapper.saveNoramlLogInfo(username, systemName,LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_INSERT.getCode(),
	                    String.format("添加设备状态编号:{%s} 操作成功!", new Object[] { devStatusId }),
	                    LogInfoDic.LOG_MODULAR_ENUM.TEMPLATE_TEST_MANAGEMENT.getMessage());
        	}
        	return devStatusId;
        }catch (Exception e){
            System.out.println("deviceStatusInfo = [" + deviceStatusInfo + "], username = [" + username + "]");
            throw e;
        }

    }

    /**
     * 修改设备状态信息表（关键字段：设备类别、设备Id、状态信息）
     *
     * @param deviceStatusInfo
     * @param username
     * @return
     */
    @Override
    public String updateDevStatusInfo(DeviceStatusInfo deviceStatusInfo, String username) {
    	try {
	    	String devStatusId = deviceStatusInfo.getDevStatusId();
        	deviceStatusInfo.setUpdateTime(TimeHelper.dateToLongString());
	    	deviceStatusInfoMapper.updateDevStatusInfo(deviceStatusInfo);
	    	pubLogMapper.saveNoramlLogInfo(username, systemName,LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_UPDATE.getCode(),
	                 String.format("修改设备状态编号:{%s} 操作成功!", new Object[] { devStatusId }),
	                 LogInfoDic.LOG_MODULAR_ENUM.TEMPLATE_TEST_MANAGEMENT.getMessage());
	        return devStatusId;
    	} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
    }

    /**
     * 获取设备状态信息
     * @param devTypeCode 设备类型编号
     * @param deviceId    设备编号
     * @return
     */
    @Override
    public DeviceStatusInfo getDevStatusInfo(String devCategoryCode, String devTypeCode, String devId) {
    	try {
	    	Map<String,Object> paramT = new HashMap<String ,Object>();
	    	paramT.put("devCategoryCode", devCategoryCode);
	    	paramT.put("devTypeCode", devTypeCode);
	    	paramT.put("devId", devId);
	    	return deviceStatusInfoMapper.getDevStatusInfo(paramT);
    	} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
    }

    /**
     * 获取设备状态信息
     * @param devStatusId 设备信息状态编号
     * @return
     */
    @Override
    public DeviceStatusInfo getDevStatusInfoById(String devStatusId) {
    	try {
	    	Map<String,Object> paramT = new HashMap<String ,Object>();
	    	paramT.put("devStatusId", devStatusId);
	    	return deviceStatusInfoMapper.getDevStatusInfoById(paramT);
    	} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
    }
    
    /**
     * 获取设备状态列表信息
     * @param devCategoryCode 设备类别编号
     * @param devTypeCode 设备类型编号
     * @param devId  设备编号
     * @param devName 设备名称
     * @param devStatusCode 设备状态编号
     * @param repStatusCode 报修状态编号
     * @return
     */
	public List<DeviceStatusInfo> selectDeviceStatusList(String devStatusId, String devCategoryCode, String devTypeCode, 
			String devId,String devName, String devStatusCode, String repStatusCode){
		try {
	    	Map<String,Object> paramT = new HashMap<String ,Object>();
	    	paramT.put("devStatusId", devStatusId);
	    	paramT.put("devCategoryCode", devCategoryCode);
	    	paramT.put("devTypeCode", devTypeCode);
	    	paramT.put("devId", devId);
	    	paramT.put("devName", devName);
	    	paramT.put("devStatusCode", devStatusCode);
	    	paramT.put("repStatusCode", repStatusCode);
	    	return deviceStatusInfoMapper.selectDeviceStatusList(paramT);
    	} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}

	@Override
	public void cleanVideoCapStatus(String devTypeCode) {
		// TODO Auto-generated method stub
		try {
			Map<String,Object> paramT = new HashMap<String ,Object>();
	    	paramT.put("devTypeCode", devTypeCode);
			deviceStatusInfoMapper.cleanVideoCapStatus(paramT);
		} catch (Exception e) {
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}
	
	@Override
	public void refreshCheckResult(DeviceStatusInfo deviceStatusInfo) {
		// TODO Auto-generated method stub
		try {
			deviceStatusInfoMapper.refreshCheckResult(deviceStatusInfo);
		} catch (Exception e) {
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}
}
